package com.aos.erp.schools.service.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @program: erp
 * @description: 同步用户RabbitMQ相关配置类
 * @author: tanzhiyu
 * @create: 2018-11-19 14:31
 **/
@Configuration
public class SynUserQueueConfig {
    /**
     * 配置交换机实例
     *
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(QueueConstants.USER_EXCHANGE_NAME);
    }

    /**
     * 配置队列实例，并且设置持久化队列
     *
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue(QueueConstants.USER_QUEUE_SAVE, true);
    }

    @Bean
    public Queue queueB() {
        return new Queue(QueueConstants.USER_QUEUE_DEL, true);
    }

    /**
     * 将队列绑定到交换机上，并设置消息分发的路由键
     *
     * @return
     */
    @Bean
    public Binding binding() {
        //链式写法: 用指定的路由键将队列绑定到交换机
        return BindingBuilder.bind(queue()).to(directExchange()).with(QueueConstants.USER_QUEUE_ROUTE_SAVE);
    }
    @Bean
    public Binding bindingB() {
        //链式写法: 用指定的路由键将队列绑定到交换机
        return BindingBuilder.bind(queueB()).to(directExchange()).with(QueueConstants.USER_QUEUE_ROUTE_DEL);
    }

    @Bean
    public Jackson2JsonMessageConverter jsonMessageConverter() {
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
        return converter;
    }
}

